package com.example.demo_homework2.service;

import ch.qos.logback.core.joran.conditional.ElseAction;
import com.example.demo_homework2.bean.CmsCategory;
import com.example.demo_homework2.mapper.CmsArticleMapper;
import com.example.demo_homework2.mapper.CmsCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.AssertFalse;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class CmsCategoryService {
    @Autowired  //跟spring容器表明，需要运行时注入一个接口对象进来；
    private CmsCategoryMapper CategoryMapper;
    @Autowired
    private CmsArticleMapper articleMapper;

    public List<CmsCategory> getAllCategory() {
        // List<CmsCategory> list = CategoryMapper.selectAll();
        List<CmsCategory> list = CategoryMapper.allCategoryWithParent();
        if (list != null) {
            return list;
        }
        return new ArrayList<CmsCategory>();
    }

    public boolean saveOrUpdate(CmsCategory cmsCategory) {
        String pid = cmsCategory.getPid();
        if (pid != null && !"".equals(pid)) {
            CmsCategory c = CategoryMapper.selectByPrimaryKey(cmsCategory.getPid());
            if (c == null) return false;
        }
        if (cmsCategory.getId() != null) {
            CategoryMapper.updateByPrimaryKey(cmsCategory);
        } else {
            cmsCategory.setId(UUID.randomUUID().toString());
            CategoryMapper.insert(cmsCategory);
        }
        return true;
    }

    /*
     *按照栏目编号删除该栏目信息
     * @param id 要删除的栏目编号
     * @return
     */
    public int deleteCategory(String id) {
        int num = CategoryMapper.selectCountByPid(id);
        System.out.println("num=" + num);
        if (num > 0) {
            return -1;
        }
        num = articleMapper.selectArticleByCategoryid(id);
        System.out.println("num=" + num);
        if (num > 0) {
            return -2;
        }
        return CategoryMapper.deleteByPrimaryKey(id);
    }

    public String deleteByIds(String[] ids) {
        ArrayList<String> suc = new ArrayList<String>();//定义一个集合记录删除成功
        ArrayList<String> fail = new ArrayList<String>();//失败
        ArrayList<String> noExist = new ArrayList<String>();//不存在
        if (ids != null && ids.length > 0) {
            for (String id : ids) {
                int result = deleteCategory(id);
                if (result == -1 || result == -2) {
                    fail.add(id);
                } else if (result == 0) {
                    noExist.add(id);
                } else {
                    suc.add(id);
                }
            }
            String str="success:";
            if(suc.size()>0){
                str +="成功删除了"+suc+"!";
            }
            if(fail.size()>0){
                str +="请先删除"+fail+"栏目下的子栏目或文章！";
            }
            if(noExist.size()>0){
                str+="栏目"+noExist+"不存在";
            }
            return str;
        }else{
            return "error:请输入要删除的栏目编号";
        }
    }
}
